Imports System.Data.SqlClient
Imports System.ComponentModel
Imports DevComponents.DotNetBar.SuperGrid
Public Class frmCatEmpleados
    Private mstrCondicion As String
    Private mstrDepartamento As String
    Private mstrPuesto As String
    Private mstrValor As String
    Private mbolSueldo As Boolean
    Private mdblSueldo As Double
    Public WriteOnly Property Condicion() As String

        Set(ByVal value As String)
            mstrCondicion = value
        End Set
    End Property
    Public WriteOnly Property MuestraSueldo() As Boolean

        Set(ByVal value As Boolean)
            mbolSueldo = value
        End Set
    End Property
    Public WriteOnly Property Departamento() As String

        Set(ByVal value As String)
            mstrDepartamento = value
        End Set
    End Property
    Public WriteOnly Property Puesto() As String

        Set(ByVal value As String)
            mstrPuesto = value
        End Set
    End Property
    Public ReadOnly Property Valor() As String
        Get
            Valor = mstrValor
        End Get
    End Property
    Public ReadOnly Property Sueldo() As Double
        Get
            Sueldo = mdblSueldo
        End Get
    End Property

    Private Sub frmCatEmpleados_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.DialogResult = Windows.Forms.DialogResult.Cancel
        End If
    End Sub

    Private Sub frmCatEmpleados_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        panelBusqueda.Style.BackColor1.Color = System.Drawing.Color.FromArgb(34, 35, 38)
        panelBusqueda.Style.BackColor2.Color = System.Drawing.Color.FromArgb(34, 35, 38)
        panelBusqueda.Style.BorderColor.Color = System.Drawing.Color.FromArgb(50, 52, 56)

        BotonesAgregar2.BackgroundStyle.BackColor = System.Drawing.Color.FromArgb(28, 28, 31)
        BotonesAgregar2.BackgroundStyle.BackColor2 = System.Drawing.Color.FromArgb(28, 28, 31)
        BotonesAgregar2.BackgroundStyle.BorderColor = System.Drawing.Color.FromArgb(28, 28, 31)
        BotonesAgregar2.BackgroundStyle.BorderColor2 = System.Drawing.Color.FromArgb(28, 28, 31)

        txtBuscarDetalle.BackColor = System.Drawing.Color.FromArgb(18, 19, 20)
        txtBuscarDetalle.ForeColor = System.Drawing.Color.FromArgb(134, 136, 140)
        BotonesAgregar2.BackgroundStyle.BackColor = System.Drawing.Color.FromArgb(18, 19, 20)
        BotonesAgregar2.BackgroundStyle.BackColor2 = System.Drawing.Color.FromArgb(18, 19, 20)
        BotonesAgregar2.BackgroundStyle.BorderColor = System.Drawing.Color.FromArgb(50, 52, 56)
        BotonesAgregar2.BackgroundStyle.BorderColor2 = System.Drawing.Color.FromArgb(50, 52, 56)

        chTodos.Checked = False

        chTodos.BackColor = Color.WhiteSmoke
        chTodos.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64)

        Me.BackColor = Color.WhiteSmoke

        txtBuscarDetalle.Clear()
        txtBuscarDetalle.Focus()
        Dim panel As GridPanel = dgEmpleados.PrimaryGrid

        Try
            If mbolSueldo = False Then
                CARGA_EMPLEADOS()
                Panel.Columns("SD").Visible = False
            Else
                CARGA_EMPLEADOS_PUESTO()
                panel.Columns("SD").Visible = True
            End If

        Catch ex As Exception

            MsgBox(ex.Message)
        End Try

    End Sub
    Sub CARGA_EMPLEADOS()
        Dim panel As GridPanel = dgEmpleados.PrimaryGrid
        dgEmpleados.BeginUpdate()
        panel.Rows.Clear()



        Dim strSql As String = ""
        Dim strNombre As String = ""

        strNombre = UCase(txtBuscarDetalle.Text.Trim)
        strNombre = "%" & strNombre.Replace(" ", "%") & "%"

        Dim CLAVE As String = BuscarStrDatoCitra("SELECT CLAVE FROM DEPARTAMENTOS WHERE UPPER(NOMBRE)='" & mstrDepartamento & "'")

        strSql = "SELECT E.CLAVE,NOMBRE_COMPLETO,P.NOMBRE,E.SD FROM EMPLEADOS E INNER JOIN PUESTOS P ON E.PUESTO=P.CLAVE WHERE DEPTO='" & CLAVE & "' AND ESTATUS<>'B' AND (UPPER(E.CLAVE) LIKE '" & strNombre & "' " & mstrCondicion & " OR UPPER(NOMBRE_COMPLETO) LIKE '" & strNombre & "' " & mstrCondicion & ")"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Dim CONT As Integer = 0

        Do While rdBuscar.Read()
            Dim ob1() As Object = {rdBuscar("CLAVE"), rdBuscar("NOMBRE_COMPLETO"), rdBuscar("NOMBRE"), rdBuscar("SD")}
            panel.Rows.Add(New GridRow(ob1))
            CONT = CONT + 1
        Loop
        panel.Footer.Text = CONT & " empleados coinciden con la busqueda"
        dgEmpleados.EndUpdate()


        If CONT = 0 Then
            btnAceptar.Enabled = False
        Else
            btnAceptar.Enabled = True
        End If

    End Sub
    Sub CARGA_EMPLEADOS_PUESTO()
        Dim panel As GridPanel = dgEmpleados.PrimaryGrid
        dgEmpleados.BeginUpdate()
        panel.Rows.Clear()



        Dim strSql As String = ""
        Dim strNombre As String = ""

        strNombre = UCase(txtBuscarDetalle.Text.Trim)
        strNombre = "%" & strNombre.Replace(" ", "%") & "%"

        Dim CLAVE As String = BuscarStrDatoCitra("SELECT CLAVE FROM DEPARTAMENTOS WHERE UPPER(NOMBRE)='" & mstrDepartamento & "'")
        Dim PUESTO As String = BuscarStrDatoCitra("SELECT CLAVE FROM PUESTOS WHERE UPPER(NOMBRE)='" & mstrPuesto & "'")

        strSql = "SELECT E.CLAVE,NOMBRE_COMPLETO,P.NOMBRE,E.SD FROM EMPLEADOS E INNER JOIN PUESTOS P ON E.PUESTO=P.CLAVE WHERE DEPTO='" & CLAVE & "' AND ESTATUS<>'B' AND PUESTO='" & PUESTO & "'AND (UPPER(E.CLAVE) LIKE '" & strNombre & "' " & mstrCondicion & " OR UPPER(NOMBRE_COMPLETO) LIKE '" & strNombre & "' " & mstrCondicion & ")"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Dim CONT As Integer = 0

        Do While rdBuscar.Read()
            Dim ob1() As Object = {rdBuscar("CLAVE"), rdBuscar("NOMBRE_COMPLETO"), rdBuscar("NOMBRE"), rdBuscar("SD")}
            panel.Rows.Add(New GridRow(ob1))
            CONT = CONT + 1
        Loop
        panel.Footer.Text = CONT & " empleados coinciden con la busqueda"
        dgEmpleados.EndUpdate()


        If CONT = 0 Then
            btnAceptar.Enabled = False
        Else
            btnAceptar.Enabled = True
        End If

    End Sub


    Sub CARGA_EMPLEADOS_TODOS()
        Dim panel As GridPanel = dgEmpleados.PrimaryGrid
        dgEmpleados.BeginUpdate()
        panel.Rows.Clear()


        Dim strSql As String = ""
        Dim strNombre As String = ""

        strNombre = UCase(txtBuscarDetalle.Text.Trim)
        strNombre = "%" & strNombre.Replace(" ", "%") & "%"

        strSql = "SELECT E.CLAVE,NOMBRE_COMPLETO,P.NOMBRE,E.SD FROM EMPLEADOS E INNER JOIN PUESTOS P ON E.PUESTO=P.CLAVE WHERE ESTATUS<>'B' AND (UPPER(E.CLAVE) LIKE '" & strNombre & "' " & mstrCondicion & " OR UPPER(NOMBRE_COMPLETO) LIKE '" & strNombre & "' " & mstrCondicion & ")"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        Dim CONT As Integer = 0

        Do While rdBuscar.Read()
            Dim ob1() As Object = {rdBuscar("CLAVE"), rdBuscar("NOMBRE_COMPLETO"), rdBuscar("NOMBRE"), rdBuscar("SD")}
            panel.Rows.Add(New GridRow(ob1))
            CONT = CONT + 1
        Loop
        panel.Footer.Text = CONT & " empleados coinciden con la busqueda"
        dgEmpleados.EndUpdate()


        If CONT = 0 Then
            btnAceptar.Enabled = False
        Else
            btnAceptar.Enabled = True
        End If

    End Sub


    Private Sub txtArticulo_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.Escape Then
            Me.DialogResult = Windows.Forms.DialogResult.Cancel
        End If
    End Sub

    Private Sub txtArticulo_TextChanged(sender As System.Object, e As System.EventArgs)
        If chTodos.Checked = True Then
            CARGA_EMPLEADOS_TODOS()
        Else
            CARGA_EMPLEADOS()

        End If
    End Sub

    Private Sub dgEmpleados_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles dgEmpleados.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.DialogResult = Windows.Forms.DialogResult.Cancel
        ElseIf e.KeyCode = Keys.Enter Then
            btnListo_Click(Me, System.EventArgs.Empty)
        End If


    End Sub

    Private Sub btnListo_Click(sender As System.Object, e As System.EventArgs)
        If dgEmpleados.PrimaryGrid.Rows.Count = 1 Then
            Dim ROW As GridRow = dgEmpleados.PrimaryGrid.GetRowFromIndex(0)
            Dim CODE As String = CStr(ROW.Cells(1).Value)
            mstrValor = CODE
            Me.DialogResult = Windows.Forms.DialogResult.OK
        Else
            For Each articulo In dgEmpleados.GetSelectedCells
                Dim celda As GridCell = articulo
                Dim ROW As GridRow = dgEmpleados.PrimaryGrid.GetRowFromIndex(celda.RowIndex)
                Dim CODE As String = CStr(ROW.Cells(1).Value)
                mstrValor = CODE
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Exit For
            Next
        End If
    End Sub

    Private Sub chTodos_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chTodos.CheckedChanged
        Dim panel As GridPanel = dgEmpleados.PrimaryGrid

        If mbolSueldo = False Then
            If chTodos.Checked = False Then
                CARGA_EMPLEADOS()
            Else
                CARGA_EMPLEADOS_TODOS()

            End If
            Panel.Columns("SD").Visible = False
        Else
            If chTodos.Checked = False Then
                CARGA_EMPLEADOS_PUESTO()
            Else
                panel.Columns("SD").Visible = True
            End If
        End If
    End Sub

    Private Sub btnAceptar_Click(sender As System.Object, e As System.EventArgs) Handles btnAceptar.Click
        If dgEmpleados.PrimaryGrid.Rows.Count = 1 Then
            Dim ROW As GridRow = dgEmpleados.PrimaryGrid.GetRowFromIndex(0)
            Dim CODE As String = CStr(ROW.Cells(1).Value)
            mstrValor = CODE
            Me.DialogResult = Windows.Forms.DialogResult.OK
        Else
            For Each articulo In dgEmpleados.GetSelectedCells
                Dim celda As GridCell = articulo
                Dim ROW As GridRow = dgEmpleados.PrimaryGrid.GetRowFromIndex(celda.RowIndex)
                Dim CODE As String = CStr(ROW.Cells(1).Value)
                Dim SUELDO As Double = CDbl(ROW.Cells(3).Value)
                mstrValor = CODE
                mdblSueldo = SUELDO
                Me.DialogResult = Windows.Forms.DialogResult.OK
                Exit For
            Next
        End If
    End Sub

    Private Sub txtBuscarDetalle_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtBuscarDetalle.TextChanged
        Dim panel As GridPanel = dgEmpleados.PrimaryGrid

        If mbolSueldo = False Then
            If chTodos.Checked = False Then
                CARGA_EMPLEADOS()
            Else
                CARGA_EMPLEADOS_TODOS()

            End If
            panel.Columns("SD").Visible = False
        Else
            If chTodos.Checked = False Then
                CARGA_EMPLEADOS_PUESTO()
            Else
                panel.Columns("SD").Visible = True
            End If
        End If
    End Sub

    Private Sub dgEmpleados_RowDoubleClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridRowDoubleClickEventArgs) Handles dgEmpleados.RowDoubleClick
        btnListo_Click(Me, System.EventArgs.Empty)
    End Sub

    Private Sub dgEmpleados_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgEmpleados.Click

    End Sub
End Class